package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.FriendShipLinkDTO;
import com.jeff.tianti.cms.dto.TeachQueryDTO;
import com.jeff.tianti.cms.entity.FriendShipLink;
import com.jeff.tianti.cms.entity.Teach;
import com.jeff.tianti.cms.service.FriendShipLinkService;
import com.jeff.tianti.cms.service.TeachService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/teach")
public class TeachController {
    @Autowired
    TeachService teachService;

    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model){
        String name = request.getParameter("name");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if(StringUtils.isNotBlank(currentPageStr)){
            currentPage = Integer.parseInt(currentPageStr);
        }
        if(StringUtils.isNotBlank(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        TeachQueryDTO teachQueryDTO = new TeachQueryDTO();
        teachQueryDTO.setName(name);
        teachQueryDTO.setCurrentPage(currentPage);
        teachQueryDTO.setPageSize(pageSize);

        PageModel<Teach> page = teachService.queryFriendShipLinkPage(teachQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("teachQueryDTO", teachQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_TEACH);
        return "/cms/teach/teach_list";
    }

    @RequestMapping("/edit")
    public String dialogEdit(HttpServletRequest request, Model model){

        String id = request.getParameter("id");
        if(StringUtils.isNotBlank(id)){
            Teach teach = teachService.find(id);
            model.addAttribute("teach", teach);
        }

        return "/cms/teach/dialog/teach_edit";
    }


    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {

            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String headImg = request.getParameter("headImg");
            String description = request.getParameter("description");
            String industry = request.getParameter("industry");
            Teach teach = new Teach();
            teach.setName(name);
            teach.setId(id);
            teach.setHeadImg(headImg);
            teach.setDescription(description);
            teach.setDeleteFlag("0");
            teach.setIndustry(industry);
            if(StringUtils.isNotBlank(id)){
                teachService.updateSelectiveById(teach);
            }else{
                teachService.save(teach);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

 /**
     * 栏目保存操作
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            teachService.delete(id);
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
